﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NavLine.Aircraft
{
	public partial class AC_Controller
	{
		public bool TowardSegment = false;
		public int SegmentIndex = -1;
		V3G[] segment;
		public double FinalDist;
		public void NavigateSegment(double segmentdist, double finaldist, params V3G[] points)
		{
			ResetControl();
			segment = new V3G[points.Length];
			points.CopyTo(segment, 0);
			SegmentIndex = 0;
			EndDist = segmentdist;
			FinalDist = finaldist;
			TowardSegment = true;
		}
		public event ACControllerEvent OnSegmentFinished;
		public void ProcessSegment(double dt)
		{
			if (TowardSegment)
			{
				if (SegmentIndex < segment.Length - 2)
				{
					NavigateLine(new Line3G(segment[SegmentIndex], segment[SegmentIndex + 1], false), EndDist);
					if (OnLineEnd == null)
						OnLineEnd += x =>
						{
							x.SegmentIndex++;
							Console.WriteLine("Switched to segment #" + x.SegmentIndex);
							TowardSegment = true;
						};
				}
				else if (SegmentIndex == segment.Length - 2)
				{
					OnLineEnd -= OnSegmentFinished;
					NavigateLine(new Line3G(segment[SegmentIndex], segment[SegmentIndex + 1], false), FinalDist);
					if (OnSegmentFinished == null)
						OnSegmentFinished += x => Console.WriteLine("Segment finished");
					OnLineEnd += OnSegmentFinished;
				}
			}
		}
	}
}